package com.mas.framework.mybatis.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.mas.framework.mybatis.core.handler.DefaultDbFieldHandler;
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;

/**
 * @author godyao
 * @description MyBaits 配置类
 * @date 2023年01月18日21:41
 */
@AutoConfiguration
@MapperScan(value = "${mas.info.basepackage}", annotationClass = Mapper.class)
public class MasMybatisAutoConfiguration {

    /**
     * 注册 mp 分页插件
     *
     * @return com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor
     * @author godyao
     * @date 2023/1/18 21:43
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        final PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        paginationInnerInterceptor.setMaxLimit(-1L);
        // 分页合理化
        paginationInnerInterceptor.setOverflow(true);
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        return mybatisPlusInterceptor;
    }

    /**
     * 注册 mp 自动填充字段处理器
     *
     * @return com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
     * @author godyao
     * @date 2023/1/18 22:12
     */
    @Bean
    public MetaObjectHandler defaultMetaObjectHandler() {
        return new DefaultDbFieldHandler();
    }
}
